﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Model
{
    /// <summary>
    /// RESPONSABILITY: - manufacturing task repository
    ///                 - create new manufacturing task
    ///                 - add task to the list of manufacturing list
    ///                 - delete a manufacturing task
    /// </summary>
    public class ChangeManufacturingTask
    {
        private List<ManufacturingTask> manufacturingTasks = new List<ManufacturingTask>();
        public IEnumerable<ITask> GetManufacturingTasks(List<string> codes = null)
        {
            if (codes == null)
            {
                return (IEnumerable<ITask>)manufacturingTasks;
            }

            var result = manufacturingTasks.Where(c => codes.Contains(c.TaskCode));

            //Console.WriteLine("GetCompanies returns " + result.Count() + " results" );

            return (IEnumerable<ITask>)result;
        }

        public ManufacturingTask CreateManufacturingTask(string taskCode, int estimatedTime, Employee employee, Machine machine)
        {
            if (string.IsNullOrEmpty(taskCode))
            {
                throw new ArgumentException("The task cod cannot be null", taskCode);
            }

          ManufacturingTask manufacturingTask = new ManufacturingTask(taskCode,estimatedTime,employee,machine);
          this.AddManufacturingTask(manufacturingTask);
          return manufacturingTask;
        }

        public void AddManufacturingTask(ManufacturingTask manufacturingTask)
        {
            if (CheckManufacturingTaskIDExistance(manufacturingTask.ManufacturingTaskId))
            {
                return;
            }

            if (CheckManufacturingTaskCodeExistance(manufacturingTask.TaskCode))
            {
                throw new Exception("A employee with the code " + manufacturingTask.TaskCode + " already exists");
            }

            manufacturingTasks.Add(manufacturingTask);

        }

        private bool CheckManufacturingTaskCodeExistance(string code)
        {
            var _task = manufacturingTasks.FirstOrDefault(t => t.TaskCode == code);

            var isFound = _task != null;

            return isFound;
        }

        private bool CheckManufacturingTaskIDExistance(int id)
        {
            var _task = manufacturingTasks.FirstOrDefault(e => e.ManufacturingTaskId == id);

            var isFound = _task != null;

            return isFound;
        }

        public void DeleteTask(ManufacturingTask manufacturingTask)
        {
            ManufacturingTask removeTask = manufacturingTasks.FirstOrDefault(t => t.ManufacturingTaskId == manufacturingTask.ManufacturingTaskId);
            if (removeTask != null)
            {
                manufacturingTasks.Remove(manufacturingTask);
            }
        }
    }
    
}
